package com.example.fresh_market.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.fresh_market.adapter.ProdCateAdmAdapter;
import com.example.fresh_market.databinding.ActivityCategoryBinding;
import com.example.fresh_market.request.ApiResponse;
import com.example.fresh_market.request.PageResult;
import com.example.fresh_market.request.ProdCateResp;
import com.example.fresh_market.utils.HttpUtil;
import com.example.fresh_market.utils.ToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class CategoryActivity extends AppCompatActivity {
    private ActivityCategoryBinding binding;
    private Context mContext;
    private ProdCateAdmAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCategoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = this;
        RecyclerView recyclerView = binding.recyclerCategory;
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ProdCateAdmAdapter(new ProdCateAdmAdapter.OnItemClickListener() {
            @Override
            public void onDeleteClick(View view, int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("删除分类");
                builder.setMessage("确定删除该分类吗？");
                builder.setNegativeButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                });
                builder.setPositiveButton("确定", (dialog, which) -> {
                    ProdCateResp prodCateResp = adapter.getItem(position);
                    String delUrl = HttpUtil.getBatchDelUrl("/admin/product/cate", List.of(prodCateResp.getId()));
                    HttpUtil.delete(mContext, delUrl, response -> {
                        ApiResponse apiResponse = new Gson().fromJson(response, ApiResponse.class);
                        if (apiResponse.getCode() == 200) {
                            ToastUtil.showToast(mContext, "删除成功");
                            initData();
                        }
                    });
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }

            @Override
            public void onItemClick(View view, int position) {

            }

            @Override
            public void onEditClick(View view, int position) {
                Intent intent = new Intent(mContext, CategoryEditActivity.class);
                intent.putExtra("cate", new Gson().toJson(adapter.getItem(position)));
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);
        initData();
        binding.addCategory.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, CategoryEditActivity.class);
            startActivity(intent);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    public void initData() {
        HttpUtil.get(mContext, "/admin/product/cate?page=1&size=10000", response -> {
            ApiResponse<PageResult<ProdCateResp>> apiResponse = new Gson().fromJson(response, new TypeToken<ApiResponse<PageResult<ProdCateResp>>>() {
            }.getType());
            if (apiResponse.getCode() == 200) {
                adapter.submitList(apiResponse.getData().getList());
            }
        });
    }
}
